<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
$return['mid']=$_SESSION['uid'];
$return['show_setmobile']=cfc('core')->check_setmobile(1);
$return['show_setmember']=cfc('core')->check_setmember(1);
$merchid=$_SESSION['merchid']?:$this->session('merchid');
$merchid=$merchid?:$_SESSION['onemerchid'];
$_SESSION['onemerchid']=$merchid;
if($_RQ['op']=='main'){
	model('jy_weishop/member')->checklogin();
	$return['shopset']=$shopset = plugin('jy_weishop')->plugin_setting('shopset');
	//营销设置
	$saleset=plugin('jy_weishop')->plugin_setting('saleset');
	$member=pdo_get('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$member2=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('avatar','nickname','credit1','credit2','mobile','realname'));
	$member=array_merge($member,$member2);
	$goodsid = intval($_RQ['goodsid']);
	if($_RQ['storeid']){
		$_SESSION['store_id']=intval($_RQ['storeid']);
	}
	$store_id=$_SESSION['store_id'];
	$sendtype=intval($_RQ['sendtype']);
	//判断是不是点餐
	$_SESSION['is_dish']=intval($_RQ['is_dish']);
	//返回链接
	$return['forward_url']=base64_encode('jy_merch/order.list//fromcreate=1');
	//下单页
	$orderid = intval($_RQ['id']);
	$order=array('tableid'=>intval($_RQ['tableid']));
	if($orderid){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'id'=>$orderid));
		$order['oldorderid']=$orderid;
	}elseif($_RQ['tableid']){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'tableid'=>$_RQ['tableid'],'status'=>0,'createtime >'=>TIMESTAMP-3*3600));
		if($order['id']){
				$table = pdo_get('jy_weishop_table',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['tableid']));
				//如果清台了，订单不算
				if(!$table['busy']){
					pdo_update('jy_weishop_order',array('status'=>4),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id'],'status'=>0));
					$order=array();
				}
		}
	}
	$order['oldorderid']=$order['id']?:0;
	$return['order_id'] = $order['id'];
	$item=array();
	//提交数据
	$return['post'] = array();
	if($merchid>0){
		$return['merch'] = pdo_get('jy_merch_user',array('uniacid' =>$_SESSION['uniacid'],'id'=>$merchid));
	}else{
		$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
		if($carrier_list){
			foreach($carrier_list as &$pv){
				$pv['pcd']=json_decode($pv['pcd'],true);
			}
			$return['carrier_list']=$carrier_list;
			$return['selectstore']=$carrier_list['0'];
		}
	}
	if($goodsid || $goods){
		$_SESSION['goodsid']=$goodsid;
		//直接购买。
		$return['post']['goodsid'] = $goodsid;
		if($goodsid){
			$goodsfile=array('id','fang_credit2','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','pcate2','ccate','scate','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),$goodsfile);
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
		}
		if($_SESSION['is_tomedia']){
			$goods['image']=$goods['image']?tomedia($goods['image']):'';
		}
		if($goodsid){
			$diyfields=model('jy_weishop/goods')->get_diyformfields($goodsid);
			//配送设置
			$goodsdispatch=model('jy_weishop/goods')->get_dispatch($goodsid);
			$goods=array_merge($goods,$goodsdispatch);
		}
		//如果来自其他商户分销（修改商户ID）
		$frommerchid=$this->session('frommerchid');
		$goods['merchid']=$frommerchid?$frommerchid:$goods['merchid'];
		if($goodsid){
			model('jy_weishop/goods')->check_buy($goods['id'],intval($_RQ['num']),intval($_RQ['optionsid']));
		}
		//多商户商品：
		if($goods['merchid'] && $merchid!=$goods['merchid']){
			$merchid=$goods['merchid'];
			$return['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['merchid']));
		}
		//购买权限：
		$levelinfo = model('jy_weishop/member')->get_level();
		if($goods['buylevels']){
			$fastvalue=substr($goods['buylevels'],0,1);
			if($fastvalue!=';'){
				$goods['buylevels']=';'.$goods['buylevels'];
			}
			$lastvalue=substr($goods['buylevels'],-1,1);
			if($lastvalue!=';'){
				$goods['buylevels']=$goods['buylevels'].';';
			}
			if(strpos($goods['buylevels'],';'.$levelinfo['id'].';')===false){
				exi('您没有权限购买该商品！','error','jy_weishop/member.open/');
			}
		}
		$groupinfo = model('jy_weishop/member')->get_group();
		if($goods['buygroups']){
			$fastvalue=substr($goods['buygroups'],0,1);
			if($fastvalue!=';'){
				$goods['buygroups']=';'.$goods['buygroups'];
			}
			$lastvalue=substr($goods['buygroups'],-1,1);
			if($lastvalue!=';'){
				$goods['buygroups']=$goods['buygroups'].';';
			}
			if(strpos($goods['buygroups'],';'.$groupinfo['id'].';')===false){
				exi('您没有权限购买该商品！','error','jy_weishop/member.open/');
			}
		}
		$goods['goodsid']=$goods['id'];
		$optionsid = intval($_RQ['optionsid']);
		$goods['storeid']=$store_id;
		$goods['optionid']=$optionsid;
		$goods['roomid']=intval($_RQ['roomid']);
		if($goodsid){
			$goods=model('jy_weishop/activity')->get_upgoods($goods);
			if($goods['active']['dian_mid']){
					$goods['merchid']=pdo_getcolumn('jy_merch_user',array('mid'=>$goods['active']['dian_mid'],'uniacid'=>$_SESSION['uniacid']),'id');
					$merchid=$goods['merchid'];
			}
		}
		//重置下单数量
		$goods['goodstotal']=$goods['total'];
		$goods['total']=intval($_RQ['num'])?:1;
		if($optionsid){
			$return['post']['optionid'] = $optionsid;
			if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
			}
			if(!$goods['options']['specs']){
				$optiontitle=$goods['options']['title'];
				$goods['istaocan'] = 1;
			}else{
				$specs = explode("_", $goods['options']['specs']);
				$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specsall){
						$optiontitle='';
						foreach($specsall as $k=>&$sp){
							if($k==0){
								$optiontitle .=$sp['title'];
							}else{
								$optiontitle .='，'.$sp['title'];
							}
						}
				}
			}
			$goods['optiondata']= $optiontitle;
		}else{
			$goods['optionid']=0;
			$return['post']['optionid'] = 0;
		}
		if($goods['active_plugin'] && !$optionsid){
			//商品在做活动，检查下库存是否不足
			model('jy_weishop/activity')->check_stock($goods['id'],$goods['active_plugin'],$goods['active_id'],$goods['total']);
		}
		$return['post']['total'] = intval($_RQ['num']);
		$order2=array(
			'sendtype'=>$order['sendtype'],
			'type'=>'one',
			'list'=>array($goods),
		);
		$order=array_merge($order,$order2);
		if($gamelogid){
			$order['gamelogid']=$gamelogid;
		}
	}else{
		//从购物车购买
		$order['sendtype']=1;//类型只能是快递配送
		if($_RQ['isbuyto']){
				$cart_list=$_SESSION['cart_list'];
				$_SESSION['cart_list']='';
				if(!$cart_list && !$order['id']){
					exi('您没有选中商品！','error','jy_merchcmi/myshop//id='.$merchid.'&tableid='.$order['tableid']);
				}
		}else{
				$cart_list=pdo_getall('jy_weishop_member_cart',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1),array('id','goodsid','total','optionid','merchid'));
				if(!$cart_list && !$order['id']){
					exi('您的购物车没有选中商品！','error','jy_merchcmi/myshop//id='.$merchid.'&tableid='.$order['tableid']);
				}
		}
		$order['cart_num']=count($cart_list);
		//如果是追加订单
		if($order['id']){
			$order['oldorderid']=$order['id'];
			$oldorders=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
			foreach($oldorders as $oov){
				$oov['isold']=1;
				$cart_list[]=$oov;
			}
		}
		$diyfields=array();
		foreach($cart_list as $k=>&$cart){
			if(!$cart['goodsid']){
				unset($cart_list[$k]);
				continue;
			}
			$goodsfile=array('id','fang_credit2','no_de_sales','isdiyprice','diyprice','invoice','type','buylevels','buygroups','title','merchid','status','pcate','pcate2','total','subtitle','image','video','price','totalcnf','weight','active_plugin','active_id','storeids','salenum','pluginname','originalprice','usermaxbuy','minbuynum','unit','isverify','diyfields','api_id','api_pluginname','api_data');
			$goods=pdo_get('jy_weishop_goods',array('id'=>$cart['goodsid']),$goodsfile);
			if(!$goods){
				exi('指定商品不存在！','error','goback');
			}
			if($merchid && $goods['merchid']!=$merchid){
				unset($cart_list[$k]);
				continue;			
			}
			$goods['api_data']=json_decode($goods['api_data'],true);
			//购买权限：
			$levelinfo = model('jy_weishop/member')->get_level();
			if($goods['buylevels']){
				$fastvalue=substr($goods['buylevels'],0,1);
				if($fastvalue!=';'){
					$goods['buylevels']=';'.$goods['buylevels'];
				}
				$lastvalue=substr($goods['buylevels'],-1,1);
				if($lastvalue!=';'){
					$goods['buylevels']=$goods['buylevels'].';';
				}
				if(strpos($goods['buylevels'],';'.$levelinfo['id'].';')===false){
					exi('您没有权限购买'.$goods['title'].'商品！','error','jy_weishop/member.open/');
				}
			}
			$groupinfo = model('jy_weishop/member')->get_group();
			if($goods['buygroups']){
				$fastvalue=substr($goods['buygroups'],0,1);
				if($fastvalue!=';'){
					$goods['buygroups']=';'.$goods['buygroups'];
				}
				$lastvalue=substr($goods['buygroups'],-1,1);
				if($lastvalue!=';'){
					$goods['buygroups']=$goods['buygroups'].';';
				}
				if(strpos($goods['buygroups'],';'.$groupinfo['id'].';')===false){
					exi('您没有权限购买'.$goods['title'].'商品！','error','jy_weishop/member.open/');
				}
			}
			if($_SESSION['is_tomedia']){
				$goods['image']=$goods['image']?tomedia($goods['image']):'';
			}
			$goods['diyfields']=model('jy_weishop/goods')->get_diyformfields($cart['goodsid']);
			if($goods['diyfields']){
				$diyfields=$goods['diyfields'];
			}
			//配送设置
			$goodsdispatch=model('jy_weishop/goods')->get_dispatch($cart['goodsid']);
			$goods=array_merge($goods,$goodsdispatch);
			model('jy_weishop/goods')->check_buy($cart['goodsid'],$cart['total'],$cart['optionid']);
			$goods['storeid']=$store_id;
			$goods['optionid']=$cart['optionid'];
			$goods=model('jy_weishop/activity')->get_upgoods($goods);
			if($goods['active']['dian_mid']){
				$goods['merchid']=pdo_getcolumn('jy_merch_user',array('mid'=>$goods['active']['dian_mid'],'uniacid'=>$_SESSION['uniacid']),'id');
				$merchid=$goods['merchid'];
			}
			//重置下单数量
			$goods['goodstotal']=$goods['total'];
			if($cart['optionid']){
				if(!$goods['options']){
					exi('指定规格的商品不存在！','error','goback');
				}
				$goods['price']=$goods['options']['price'];
				$specs = explode("_", $goods['options']['specs']);
				$specsall = pdo_getall('jy_weishop_goods_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				if($specsall){
					$optiontitle='';
					foreach($specsall as $k2=>$sp){
						if($k2==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
				}
				$goods['optiondata']= $optiontitle;
			}
			//来自商户的分销
			$cart['merchid']=$cart['merchid']?$cart['merchid']:$goods['merchid'];
			if($goods['type']==1 || $goods['type']==2){
				//虚拟商品
				$order['sendtype']=3;
			}elseif($goods['type']==3){
				//计时计次服务，发货类型必须是上门核销
				$order['sendtype']=2;
			}elseif($goods['type']==0 || $goods['isverify']){
				//实体商品，查找是否有门店，如果有门店则表示支持自提，否则只能是快递配送
				if(!$carrier_list && !$merchid){
					$order['sendtype']=1;
				}else{
					$return['sendtypes']=array(
						'1'=>'快递配送',
						'2'=>'自提',
					);
				}
			}
			$cart=array_merge($goods,$cart);
			$cart['cartid']=$cart['id'];
			$cart['id']=$goods['id'];
		}
		$cart_list=array_values($cart_list);
		$order2=array(
			'sendtype'=>$order['sendtype'],
			'type'=>'cart',
			'list'=>$cart_list,
		);
		$order=array_merge($order,$order2);
	}
	$return['citypeisong']=plugin('jy_weishop')->plugin_setting('citypeisong');
	if($return['citypeisong']['isopen']){
		$order['ismake']=$return['citypeisong']['ismake'];
	}
	if($merchid){
		$order['merchid']=$merchid;
		if(empty($return['merch'])){
			$return['merch']=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
		}
		$order['ismake']=$return['merch']['ismake'];
		if($return['merch']['sendtype']){
			$sendtype=$return['merch']['sendtype'];
		}
	}
	$order['sendtype']=$sendtype?:$order['sendtype'];
	if(empty($order['list'])){
		exi('请先选择商品再下单！','error','jy_merch/member.cart/');
	}
	//统计订单商品总数和总价
	$total=0;
	$total_price=0.00;
	$oldfee=0.00;
	$total_weight=0;
	$buyxiu_de_price=0;
	$fang_credit2=0;
	//打包费
	$order['out_upfee']=0;
	$grouplistold=array();
	$grouplist=array();
	foreach($order['list'] as &$ov){
		if($ov['merchid']){
			$isid=pdo_getcolumn('jy_merch_user',array('id'=>$ov['merchid'],'uniacid'=>$_SESSION['uniacid']),'id');
			if(!$isid){
				$order['exitnomerch']=1;
			}
		}else{
			$order['exitnomerch']=1;
		}
		//返现
		if($ov['fang_credit2']){
			if(substr($ov['fang_credit2'],-1)=='%'){
				$ov['fang_credit2']=($ov['total']*$ov['price']*substr($ov['fang_credit2'],0,-1))/100;
			}else{
				$ov['fang_credit2']=$ov['total']*$ov['fang_credit2'];
			}
		}
		$total+=$ov['total'];
		$fang_credit2+=$ov['fang_credit2'];
		$total_price+=$ov['total']*$ov['price'];
		$oldfee+=$ov['total']*$ov['originalprice'];
		$total_weight+=$ov['weight']*$ov['total'];
		$buyxiu_de_price+=$ov['buyxiu_de_price'];
		if($ov['api_data']['out_upfee']){
			$order['out_upfee'] +=$ov['total']*$ov['api_data']['out_upfee'];
		}else if($return['merch']['out_upfee']>0){
			$order['out_upfee'] +=$ov['total']*$return['merch']['out_upfee'];
		}
		$pcate2=$ov['pcate2']?:$ov['pcate'];
		if($ov['isold']){
			if(!$grouplistold[$pcate2]['title']){
				$grouplistold[$pcate2]['title']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$pcate2,'uniacid'=>$_SESSION['uniacid']),'title');
			}
			$grouplistold[$pcate2]['list'][]=$ov;
		}else{
			if(!$grouplist[$pcate2]['title']){
				$grouplist[$pcate2]['title']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$pcate2,'uniacid'=>$_SESSION['uniacid']),'title');
			}
			$grouplist[$pcate2]['list'][]=$ov;
		}
	}
	$return['grouplist']=array_values($grouplist);
	$return['grouplistold']=array_values($grouplistold);
	$order['out_upfee']=intval($order['out_upfee']*100)/100;
	$order['fang_credit2']=$fang_credit2;
	$order['total']=$total;
	$order['total_price']=$gamelogid?0:$total_price;
	$order['oldfee']=$oldfee;
	$order['total_weight']=round(($total_weight/1000)*1000,2);
	if($order['merchid']){
		$getsendtypes=model('jy_merch/model')->sendtypes($order['merchid'],$return['sendtypes'],true,$order['is_peisong']);
		$return['sendtypes']=$getsendtypes['sendtypes'];
		$order['ismake']=$getsendtypes['ismake'];
		$order['sendtype']=$getsendtypes['default'];
		$order['is_peisong']=$getsendtypes['is_peisong'];
		$return['onemerchid']=$order['merchid'];
	}
	if(count($return['sendtypes'])==1){
		$order['sendtype']=$return['sendtypes'][1]?1:2;
	}
	//点餐机制：
	//凭号下单
	$order['tableid'] = $order['tableid']?:intval($_RQ['tableid']);
	if($order['tableid']){
		$return['table'] = pdo_get('jy_weishop_table',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'id'=>$order['tableid']));
		$order['sendtype']=2;
		$return['sendtypes']=array(
			'2'=>'扫码点餐',
		);
		if($return['merch']['is_moregoods']){
			$return['is_moregoods']=1;
		}
	}
	if($store_id){
		$return['selectstore']=pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$store_id));
	}
	if($return['selectstore']['id']){
		$return['selectstore']['juli']=1;
	}
	//提交数据
	$return['post']['is_peisong'] = $order['is_peisong'];
	$return['post']['invoice'] = $return['shopset']['invoice'];
	$return['post']['address'] = $item['address']?:array('city'=>'');
	$return['post']['merchid'] = $merchid;
	$return['post']['make_type'] = 0;
	$return['post']['dayid'] = 0;
	$return['post']['remark'] =$order['remark'];
	$return['post']['storeid'] = $return['selectstore']['id'];
	/*
		赠品
		先根据订单金额找赠品，再根据商品找赠品
	*/
	$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>0));
	//$current_gift=array();
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['orderprice'] && $gift['orderprice']<$order['no_dispatch_price']){
				if(!$current_gift || $current_gift['orderprice']<$gift['orderprice']){
					$current_gift=$gift;
				}
			}
		}
	}
	if(!$current_gift){
		$goods_gifts=pdo_getall('jy_weishop_goods_gift',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'activity'=>1));
	}
	if($goods_gifts){
		foreach($goods_gifts as $gift){
			$gift['onlinetime']=json_decode($gift['onlinetime'],true);
			$gift['goodsid']=json_decode($gift['goodsid'],true);
			if($gift['onlinetime']['tstart']<time() && $gift['onlinetime']['tend']>time() && $gift['goodsid']){
				foreach($order['list'] as $goods){
					foreach($gift['goodsid'] as $gv){
						if($goods['goodsid']==$gv['id']){
							$current_gift=$gift;
							break;
						}
					}
				}
			}
			if($current_gift){
				break;
			}
		}
	}
	if($current_gift){
		$order['gift_id']=$current_gift['id'];
		$return['gifts']=json_decode($current_gift['gifts'],true);
		foreach($return['gifts'] as &$gv){
			$gv['price']=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['id'],'uniacid'=>$_SESSION['uniacid']),'price');
			$gv['image']=tomedia($gv['image']);
		}
	}
	$order['total_price']=round($order['total_price'],2);
	$_SESSION['order']=$return['order']=$order;
	$item['sendtype']=1;
	$return['item']=$item;
	$return['member']=$member;
	set_html('title','确认订单');
	//获取小程序订阅消息ID
	$return['tmplIds']=array();
	$return['openid']=$_SESSION['openid'];
	//小程序自定义表单用到
	$return['nowday']=date('Y-m-d');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$this->setting=plugin('jy_merch')->plugin_setting('setting');
	$post=get_postdata();
	//整理订单数据
	$order=$_SESSION['order'];
	if(empty($order)){
		exi('订单异常，请刷新页面！','error');
	}
	if(!$post['merchid'] && $post['sendtype']==2 && $post['is_work_time'] && empty($post['tostoretime'])){
		//exi('请选择到店时间！','error');
	}
	$new_order=array(
		'mid'=>$_SESSION['uid'],
		'goodsprice'=>$order['total_price'],
		'remark'=>trim($post['remark']),
		'goods'=>$order['list'],
		'merchid'=>$order['merchid'],
	);
	$post['storeid']=0;
	$new_order['storeid']=intval($post['storeid']);
	if($order['merchid']){
		$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['merchid']),array('onlinetime','ismake','isjiedan','mypeisong','lat_lng'));
	}
	$new_order['price']=$new_order['price']>0?$new_order['price']:0;
	if($new_order['price']<0){
		exi('订单金额不能小于零！','error');
	}
	if($order['gift_id']){
		$new_order['gift_id']=$order['gift_id'];
	}
	if($_SESSION['is_dish']){
		$new_order['isdish']=1;
	}
	if($merch['mypeisong']){
		$new_order['send_status']=17;
	}
	//自动接单
	if($merch['isjiedan']==1){
		$new_order['send_status']=16;
	}
	//如果需要制作，配送状态为未制作！
	if($order['ismake']==1){
		$new_order['send_status']=11;
	}
	//拼单部分
	if($this->session('teamid')){
		$new_order['teamid']=$teamid;
		$new_order['is_team']=1;
		$new_order['plugin_name']=$this->session('plugin_name');
	}
	//餐桌
	$new_order['tableid']=$order['tableid'];
	if($new_order['tableid'] && $this->session('usernum')){
		$new_order['api_data']=array(
			'usernum'=>$this->session('usernum'),
			'childnum'=>$this->session('childnum')
		);
	}
	//剔除已经追加的商品
	if($order['oldorderid']){
		$new_order['oldorderid']=$order['oldorderid'];
		foreach($new_order['goods'] as $k=>$ov){
			if($ov['isold']==1){
				unset($new_order['goods'][$k]);
			}
		}
	}elseif($new_order['tableid']){
		$oldorder=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'tableid'=>$new_order['tableid'],'status'=>0,'createtime >'=>TIMESTAMP-3*3600),array('id','price'));
		if($oldorder['id']){
			$new_order['oldorderid']=$oldorder['id'];
			$new_order['price'] +=$oldorder['price'];
			$new_order['goodsprice'] +=$oldorder['price'];
		}
	}
	$order_id=model('jy_weishop/order')->create($new_order);
	if($new_order['tableid']){
		pdo_update('jy_weishop_table',array('up_time'=>time(),'is_get'=>0,'busy'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$new_order['tableid']));
	}
	unset($_SESSION['order']);
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core' && $_SESSION['current_plugin']!='jy_weishop'){
		$success_url=plugin($_SESSION['current_plugin'])->weishop_order_detail;
		if($success_url){
			$success_url=$_SESSION['current_plugin'].$success_url.'/id='.$order_id;
			$error_url=$success_url;
		}
	}
	$params=array(
		'id'=>$order_id,
		'title'=>'购买商品',
		'fee'=>$new_order['price'],
		'oldfee'=>$order['oldfee'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_merch/order.create/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>$success_url?:'jy_merch/order.list/',
		'error_url'=>$error_url?:'jy_merch/order.list/',
	);
	//虚拟设置
	$plugin_xuni=model('jy_weishop/activity')->plugin_xuni($new_order);
	if(count($plugin_xuni)){
		$params['plugin_xuni']=$plugin_xuni;
	}
	if($this->setting['isone_credit'] && $new_order['merchid']){
		$params['merchid']=$new_order['merchid'];
	}
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_weishop_order',array('pay_id'=>$payid),array('id'=>$order_id));
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
	);
	if(!$post['is_moregoods']){
		$resurl='jy_merchcmi/order.create//id='.$order_id;
		exi('','',$resurl);
	}
	exi($res);
}
if($_RQ['op']=='check_buy'){
	$merchid=intval($_RQ['merchid']);
	$sendtype=intval($_RQ['sendtype']);
	$tableid=intval($_RQ['tableid']);
	$oldorderid=$this->session('oldorderid');
	$prams=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'selected'=>1);
	if($merchid){
		if($merchid==-1){
			$prams['merchid']=0;
		}else{
			$prams['merchid']=$merchid;
		}
	}
	$cart_list=pdo_getall('jy_weishop_member_cart',$prams,array('id','goodsid','total','optionid','merchid'));
	if(!$cart_list && !$oldorderid){
		exi('您的购物车没有选中商品！','error');
	}
	model('jy_weishop/cart')->check_buy($cart_list);
	model('jy_weishop/activity/sales')->check_sale_new($cart_list);
	$query='sendtype='.$sendtype.'&merchid='.$merchid.'&tableid='.$tableid.'&id='.$oldorderid;
	if($oldorderid){
		$query .='&goodslist=1';
	}
	$resurl='jy_merchcmi/order.create//'.$query;
	if($_RQ['isresurl']){
		exi($resurl);
	}else{
		exi('','',$resurl);
	}
}
?>